package guda.house.web.action.sys;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.mvc.form.Form;
import guda.grape.util.RequestUtil;
import guda.house.biz.OfficeEmployeeBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.form.OfficeEmployeeEditForm;
import guda.house.biz.form.OfficeEmployeeForm;
import guda.house.biz.query.OfficeEmployeeQuery;
import guda.house.biz.query.SysVarQuery;
import guda.house.biz.query.VillageQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.OfficeEmployeeDAO;
import guda.house.dal.dataobject.OfficeEmployeeDO;
import guda.house.dal.dataobject.SysVarDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by well on 16/1/12.
 */
@Controller
public class EmployeeAction {

    @Autowired
    private OfficeEmployeeBiz officeEmployeeBiz;
    @Autowired
    private OfficeEmployeeDAO officeEmployeeDAO;

    @RequestMapping(value = "sys/employee/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        OfficeEmployeeQuery officeEmployeeQuery = new OfficeEmployeeQuery();
        officeEmployeeQuery.setPageNo(pageId);
        officeEmployeeQuery.setPageSize(pageSize);
        modelMap.put("query", officeEmployeeQuery);
        BizResult bizResult = officeEmployeeBiz.list(officeEmployeeQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/employee/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }

    @RequestMapping(value = "sys/employee/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, OfficeEmployeeEditForm officeEmployeeEditForm,
                       BindingResult result) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = officeEmployeeBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            officeEmployeeEditForm.initForm(((OfficeEmployeeDO) bizResult.data.get("officeEmployeeDO")));
            bizResult = officeEmployeeBiz.queryForCreate(officeEmployeeEditForm.getVillageId());
            modelMap.putAll(bizResult.data);
            return "sys/employee/edit.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/employee/detail.htm", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = officeEmployeeBiz.detail(id);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/employee/detail.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/employee/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, OfficeEmployeeForm officeEmployeeForm,
                         BindingResult result) {
        return "sys/employee/create.vm";
    }

    @Form
    @RequestMapping(value = "sys/employee/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap, @Valid OfficeEmployeeForm officeEmployeeForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = officeEmployeeBiz.queryForCreate(officeEmployeeForm.getVillageId());
            modelMap.putAll(bizResult.data);
            return "sys/employee/create.vm";
        }
        OfficeEmployeeDO officeEmployeeDO = officeEmployeeForm.toDO();
        BizResult bizResult = officeEmployeeBiz.create(officeEmployeeDO);
        if (bizResult.success) {
            return "redirect:/sys/employee/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/employee/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
                           @Valid OfficeEmployeeEditForm officeEmployeeEditForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = officeEmployeeBiz.queryForCreate(officeEmployeeEditForm.getVillageId());
            modelMap.putAll(bizResult.data);
            return "sys/employee/edit.vm";
        }
        OfficeEmployeeDO officeEmployeeDO = officeEmployeeEditForm.toDO();
        BizResult bizResult = officeEmployeeBiz.update(officeEmployeeDO);
        if (bizResult.success) {
            return "redirect:/sys/employee/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "sys/employee/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = officeEmployeeBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }


    }

    @RequestMapping(value = "sys/employee/getEmployee.htm")
    public void getEmployee(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long varId = RequestUtil.getLong(request, "varId");
        JsonResult result = JsonResult.getInstance();

        OfficeEmployeeQuery officeEmployeeQuery = new OfficeEmployeeQuery();
        officeEmployeeQuery.setPageSize(500);
        officeEmployeeQuery.setVarId(varId);
        List<OfficeEmployeeDO> officeEmployeeDOList = officeEmployeeDAO.selectForPage(officeEmployeeQuery);
        result.getMap().put("employeeList", officeEmployeeDOList);
        ResponseHelper.ajaxReturn(result.getMap(), response);
        return;


    }
}
